package eu.sudparis.it.opp.ex;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import eu.sudparis.it.opp.*;

public class TimeSeriesLoader {
	long min = Long.MAX_VALUE;
	long max = Long.MIN_VALUE;
	int max_id = Integer.MIN_VALUE;
	int min_id = Integer.MAX_VALUE;
	public TraceFilter filter = new TraceFilter() {

		@Override
		public boolean filter(int ida, int idb, long start_time, long end_time) {
			// TODO Auto-generated method stub
			return true;
		}

	};

	public static void main(String[] args) {
		new TimeSeriesLoader().load(args);
	}

	public TimeSeries load(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(args[0]));
			List<EncounterEvent> encounter = new LinkedList<EncounterEvent>();
			List<LeaveEvent> leave = new LinkedList<LeaveEvent>();
			List<CommunicationEvent> coms = new LinkedList<CommunicationEvent>();
			readTraceFile(br, encounter, leave);
			if(args.length>=2){
				BufferedReader sr=new BufferedReader(new FileReader(args[1]));
				readUserComFile(sr,coms);
			}
			TimeSeries ts = new TimeSeries();
			ts.min_time = min;
			ts.max_time = max;
			ts.min_id = min_id;
			ts.max_id = max_id;
			for (EncounterEvent ee : encounter) {
				ts.attachEvent(ee.occur, ee);
			}
			for (CommunicationEvent ee : coms) {
				ts.attachEvent(ee.occur, ee);
			}
			for (LeaveEvent le : leave) {
				ts.attachEvent(le.occur, le);
			}
			System.out.println("Min_ID:" + min_id + "\tMax_ID:" + max_id);
			return ts;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private void readUserComFile(BufferedReader br, 
			List<CommunicationEvent> coms) throws IOException{
		String rl=br.readLine();
		do {
			String[] rows = rl.split("\t");
			CommunicationEvent cevent=new CommunicationEvent();
			cevent.occur=Integer.parseInt(rows[0]);
			cevent.sender=Integer.parseInt(rows[1]);
			for(int i=2;i<rows.length-1;i++){
				cevent.receivers.add(Integer.parseInt(rows[i]));
			}
			cevent.timeslot=Integer.parseInt(rows[rows.length-1]);
			rl = br.readLine();
		} while (rl != null);
	}

	private void readTraceFile(BufferedReader br,
			List<EncounterEvent> encounter, List<LeaveEvent> leave)
			throws IOException {
		String rl = br.readLine();
		do {
			String[] rows = rl.split("\t");
			if (this.filter.filter(Integer.parseInt(rows[0]),
					Integer.parseInt(rows[1]), Long.parseLong(rows[2]),
					Long.parseLong(rows[3]))) {
				EncounterEvent ee = new EncounterEvent();
				LeaveEvent le = new LeaveEvent();
				ee.observer = Integer.parseInt(rows[0]);
				ee.observee = Integer.parseInt(rows[1]);
				ee.occur = Long.parseLong(rows[2]);
				if (ee.occur < min) {
					min = ee.occur;
				}
				encounter.add(ee);
				le.observer = Integer.parseInt(rows[0]);
				le.observee = Integer.parseInt(rows[1]);
				le.occur = Long.parseLong(rows[3]);
				if (le.occur > max) {
					max = le.occur;
				}
				leave.add(le);
				if (ee.observer < min_id) {
					min_id = ee.observer;
				}
				if (ee.observer > max_id) {
					max_id = ee.observer;
				}
				if (ee.observee < min_id) {
					min_id = ee.observee;
				}
				if (ee.observee > max_id) {
					max_id = ee.observee;
				}
			}
			rl = br.readLine();
		} while (rl != null);
	}
}
